﻿using EBA.Aspects;
using EBA.IoC;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace EBA.Tests.IoC
{
    [TestClass]
    public class ContainerConfigurationTest
    {
        [TestMethod]
        public void only_one_export_convention_is_available_for_same_contractname_and_contracttype()
        {
            var config = new ContainerConfiguration();
            config.WithPart<MultiExport>();
            config.WithPart<MultiExport>();

            var container = config.CreateContainer();

            var exports = container.Conventions.Exports[typeof(IMultiExport)];

            Assert.AreEqual(1, exports.Count());
        }

        #region Only_One_Export_Convention_Is_Available_For_Same_Export
        public interface IMultiExport { }

        [Export(typeof(IMultiExport))]
        public class MultiExport : IMultiExport { }
        #endregion

        [TestMethod]
        public void a_type_can_export_multi_exports_with_same_contracttype_and_difference_contractname()
        {
            var config = new ContainerConfiguration();
            config.WithPart<MultiNamedExport>();
            config.WithPart<MultiNamedExport>();

            var container = config.CreateContainer();

            var exports = container.Conventions.Exports[typeof(IMultiNamedExport)];

            Assert.AreEqual(2, exports.Count());
            Assert.AreEqual(1, exports.Count(i => i.ContractName == null));
            Assert.AreEqual(1, exports.Count(i => i.ContractName == "Named"));
        }

        public interface IMultiNamedExport { }
        [Export("Named", typeof(IMultiNamedExport))]
        [Export(typeof(IMultiNamedExport))]
        public class MultiNamedExport : IMultiNamedExport
        {
            public void Invoke(string obj1, string obj2)
            {
                var context = new InterceptionContext();
                context.Parameters = new object[] { obj1, obj2 };

            }

        }

    }
}
